home *** CD-ROM | disk | FTP | other *** search
- // This block outputs the results of an equation.
- // Copyright © 1989-1994 by Imagine That, Inc.
- // All Rights Reserved.
- // Extend Generic Library; Bob Diamond 1/92
- //
- // modified: 7/17/92 BD - fixed StrPart index at start of string
- // 1/17/94 JSL added tempSeed and mySeed
- // 2/16/94 DJK added trace and report
- // 3/10/94 DJK added block label to report & trace
-
-
- real timeArray[];
- Integer dynArrayFunc[];
- String oldEquation, oldLabels;
- integer mySeed, tempSeed;
-
- // This message occurs for each step in the simulation.
- on simulate
- {
- // save and restore a global and local seed,
- // in case the equation contains random numbers
-
- tempSeed = RandomGetSeed();
- RandomSetSeed(mySeed);
- ConOut = EquationCalculate(con1in, con2in, con3in, con4in, con5in, 0.0, 0.0,
- 0.0, 0.0, 0.0, dynArrayFunc);
- mySeed = RandomGetSeed();
- RandomSetSeed(tempSeed);
-
- ** sysglobal2 is the file reference number for the DEBUG TRACE
- if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal2,"Equation block number "+(MyBlockNumber())+". Current Time:"+currentTime+".","",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- fileWrite(sysGlobal2," "+label1+" = "+con1In,"",True);
- fileWrite(sysGlobal2," "+label2+" = "+con2In,"",True);
- fileWrite(sysGlobal2," "+label3+" = "+con3In,"",True);
- fileWrite(sysGlobal2," "+label4+" = "+con4In,"",True);
- fileWrite(sysGlobal2," "+label5+" = "+con5In,"",True);
- fileWrite(sysGlobal2," Output = "+conOut,"",True);
- fileWrite(sysGlobal2," ","",True);
- }
-
- }
-
-
- Procedure
- CompileIfNecessary()
- {
- String newStr;
- integer err, sizeOfCompiled;
-
- sizeOfCompiled = GetDimension(dynArrayFunc);
-
- newStr = label1+label2+label3+label4+label5+Output;
-
- // check to see if user has changed any equation related fields
- if (sizeOfCompiled == 0 || newStr != oldLabels || equation != oldEquation)
- {
- err = EquationCompile(label1, label2, label3, label4, label5, "", "",
- "", "", "", Output, Equation, 8, dynArrayFunc);
-
- if (err)
- {
- oldLabels = ""; // error, force recompile
- oldEquation = ""; // error, force recompile
-
- abort; // don't close the dialog box
- }
- }
-
- // good compile
- oldLabels = newStr; // save changed strings for later comparison
- oldEquation = equation;
- }
-
-
- // this function returns TRUE if it finds the label in the equation
- integer
- FindInEquation(string label)
- {
- integer index;
- string beginStr, endStr, remainingStr;
-
- if (label == "")
- return(FALSE); // empty string, return not found
-
- remainingStr = equation;
-
- while (TRUE)
- {
- index = strfind(remainingStr, label, false, false);
-
- if (index >= 0) // label was found
- {
- // test for delimiter at beginning and end
- if (index == 0) // special case at beginning of str
- beginStr = ""; // simulate delimiter
- else
- beginStr = strpart(remainingStr, index-1, 1);
-
- endStr = strpart(remainingStr, index+strlen(label), 1);
-
- // test for delimiter. If found, return TRUE
- if ((beginStr < "A" || beginStr > "Z") &&
- (beginStr < "a" || beginStr > "z") &&
- (beginStr < "0" || beginStr > "9") &&
- beginStr != "_" &&
- (endStr < "A" || endStr > "Z") &&
- (endStr < "a" || endStr > "z") &&
- (endStr < "0" || endStr > "9") &&
- endStr != "_")
- return(TRUE);
- }
- else
- return(FALSE);
-
- remainingStr = StrPart(remainingStr, index+1, 255);
- }
-
- return(FALSE); // not found
- }
-
-
-
- ** If the dialog data is inconsistent for simulation, abort.
- on ok
- {
- CompileIfNecessary(); // check if compile is needed here
- }
-
- on checkData
- {
- String errorStr;
-
- errorStr = "";
-
- CompileIfNecessary(); // check if compile is needed here
-
- // now check the connections
- // if a label is used in equation, check the input for connected
-
- if (FindInEquation(label1) && !Con1In) // con1in isn't connected but var is used
- errorStr = "Input1";
- else if (FindInEquation(label2) && !Con2In) // con2in isn't connected but var is used
- errorStr = "Input2";
- else if (FindInEquation(label3) && !Con3In) // con3in isn't connected but var is used
- errorStr = "Input3";
- else if (FindInEquation(label4) && !Con4In) // con4in isn't connected but var is used
- errorStr = "Input4";
- else if (FindInEquation(label5) && !Con5In) // con5in isn't connected but var is used
- errorStr = "Input5";
-
- if (errorStr != "") // if error
- {
- usererror(errorStr + " isn't connected and it is used in the equation \
- in Equation block number "+MyBlockNumber());
-
- abort; // cancel simulation
- }
-
- // now check for connections that are not used in the equation
- if (!FindInEquation(label1) && Con1In) // con1in is connected but var not used
- errorStr = "Input1";
- else if (!FindInEquation(label2) && Con2In) // con2in is connected but var not used
- errorStr = "Input2";
- else if (!FindInEquation(label3) && Con3In) // con3in is connected but var not used
- errorStr = "Input3";
- else if (!FindInEquation(label4) && Con4In) // con4in is connected but var not used
- errorStr = "Input4";
- else if (!FindInEquation(label5) && Con5In) // con5in is connected but var not used
- errorStr = "Input5";
-
- if (errorStr != "") // if error
- {
- usererror(errorStr + " is connected but it isn't used in the equation \
- in Equation block number "+MyBlockNumber());
-
- abort; // cancel simulation
- }
- }
-
- ** Initialize data for block
- on CreateBlock
- {
- label1 = "Var1";
- label2 = "Var2";
- label3 = "Var3";
- label4 = "Var4";
- label5 = "Var5";
- Output = "Result";
- equation = "Result = Var1 + Var2 + Var3 + Var4 + Var5;";
- oldEquation = "";
- oldLabels = "";
- }
-
-
- ** User clicked the dialog Functions button.
- on FunctionsAlpha
- {
- showFunctionHelp(TRUE);
- }
-
-
- ** User clicked the dialog Functions button.
- on FunctionsType
- {
- showFunctionHelp(FALSE);
- }
-
-
- on initSim
- {
- if (randomSeed == 0)
- mySeed = RandomGetSeed() + myBlockNumber()+1;
- else
- mySeed = randomSeed + myBlockNumber()+1;
-
- if( getPassedArray(sysGlobal0, timeArray) )
- getSimulateMsgs(FALSE);
- }
-
- on endSim
- {
- ** sysGlobal1 is the file reference number for the TEXT REPORT
- if( sysGlobal1 != 0.0 ) ** 0 is error, check for open file for REPORT
- {
- // template for report: |BLOCK NAME *****************|block number |BLOCK NUMBER*******
- fileWrite(sysGlobal1,"Equation block number "+MyBlockNumber(),"",True);
- if(getBlockLabel(myBlockNumber()) != "")
- fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
- fileWrite(sysGlobal1,Equation,"",True);
- if( comments != "" )
- fileWrite(sysGlobal1," Comments = "+comments,"",True);
- fileWrite(sysGlobal1," ","",True);
- }
- }